/*
 * @Author: zhangrongqiu
 * @Date: 2025-10-19 15:51:54
 * @LastEditTime: 2025-11-02 17:21:13
 * @LastEditors: zhangrongqiu
 * @Description:
 */
import React, { useEffect, useState } from "react";
import { useDidShow, useDidHide } from "@tarojs/taro";
import Taro from "@tarojs/taro";
import { ConfigProvider } from "@nutui/nutui-react-taro";
import enUS from "@nutui/nutui-react-taro/dist/locales/en-US";
import zhCN from "@nutui/nutui-react-taro/dist/locales/zh-CN";
import "@nutui/nutui-react-taro/dist/style.css";
// 全局样式
import "./app.scss";

// 全局状态管理
let globalLocale = zhCN;

export const setGlobalLocale = (locale) => {
  globalLocale = locale;
};

export const getGlobalLocale = () => globalLocale;

function App(props) {
  const [locale, setLocale] = useState(zhCN);
  // 可以使用所有的 React Hooks
  useEffect(() => {
    // 使用同步方式获取存储
    const savedLanguage = Taro.getStorageSync("appLanguage");
    if (savedLanguage === "enUS") {
      setLocale(enUS);
      globalLocale = enUS;
    } else {
      setLocale(zhCN);
      globalLocale = zhCN;
    }
  }, []);

  const switchLocale = () => {
    const newLocale = locale === zhCN ? enUS : zhCN;
    setLocale(newLocale);
    globalLocale = newLocale;
    // 保存到本地存储
    Taro.setStorage({
      key: "appLanguage",
      data: newLocale === enUS ? "enUS" : "zhCN",
    });
  };

  // 对应 onShow
  useDidShow(() => {});

  // 对应 onHide
  useDidHide(() => {});

  return <ConfigProvider locale={locale}>{props.children}</ConfigProvider>;
}

export default App;
